package cn.cboard.forum;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import cn.cboard.model.ForumThread;
import cn.cboard.service.ServiceFactory;
import cn.cboard.service.ThreadService;
import cn.cboard.util.ParamUtils;

public class PostWebHandler {
	private final static Logger logger=Logger.getLogger(PostWebHandler.class);
	public PostWebHandler(){}

	public void newPost(HttpServletRequest request, HttpServletResponse response) {
		int forumid = ParamUtils.getIntParameter(request, "forumid", 1);
		request.setAttribute("forumid", forumid);				
	}

	public void listPost(HttpServletRequest request,
			HttpServletResponse response) {
		logger.debug("[boardmx]--> PostWebHandle listPost()");
		/*
		Long threadid = ParamUtils.getLongParameter(request, "threadid", new Long(1));		
		PostService postService = null;//ServiceFactory.getPostService();		
		Collection<Post> postlist = postService.listPost(threadid);
		request.setAttribute("threadid", threadid);
		request.setAttribute("postlist", postlist);
		*/
	}

	public void replyPost(HttpServletRequest request,
			HttpServletResponse response) {
		Long threadid = ParamUtils.getLongParameter(request, "threadid", new Long(1));

		ThreadService threadService = ServiceFactory.getThreadService();
		ForumThread forumThread = threadService.getThread(threadid);
		
		request.setAttribute("threadid", threadid);
		request.setAttribute("thread", forumThread);
	}

	public void saveReplyPost(HttpServletRequest request,
			HttpServletResponse response) {
		Long threadid = ParamUtils.getLongParameter(request, "threadid", new Long(1));
		String topic = ParamUtils.getParameter(request, "topic");
		logger.debug("[boardmx]--> SaveReplyPost():" + threadid);
		logger.debug("[boardmx]--> SaveReplyPost():" + topic);
		
	}

	public void savePost(HttpServletRequest request,
			HttpServletResponse response) {
		//Long forumid = ParamUtils.getLongParameter(request, "forumid", new Long(1));
		Long forumid= Long.parseLong(request.getParameter("forumid"));
		
		
		String topic = ParamUtils.getParameter(request, "topic");		
		String message = ParamUtils.getParameter(request, "message");
		
		System.out.println(">>>>>>>>>>>>>:"+forumid);
		
		//PostService postService = null; //ServiceFactory.getPostService();
		ThreadService threadService = ServiceFactory.getThreadService();
		
		ForumThread forumThread = new ForumThread();
		//forumThread.setForumID(forumid);
		//forumThread.setTopic(topic);
		//forumThread.setUserID(new Long(1)); // for test
		
		//Long threadID = threadService.createThread(thread);
		
		//System.out.println(threadID);
		/*
		Post post = new Post();
		post.setTopic(topic);
		post.setParentPostID(new Long(0));
		post.setMessage(message);
		post.setUserID(new Long(1));
		Long date = System.currentTimeMillis();
		post.setPosted(date);
		*/
		//post.setThreadID(threadID);
		
		//boolean isSaved = postService.createPost(post);
		
		
		//request.setAttribute("issaved", isSaved);
	}
}